﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;

namespace WoChu.RfClient.Duty
{
    public partial class FrmExpiryActivate : Form
    {
        public FrmExpiryActivate()
        {
            InitializeComponent();
        }
        private static List<LocationInventoryInfo> _lstLocationInventory = null;
        public static List<LocationInventoryInfo> lstLocationInventory
        {
            get
            {
                if (_lstLocationInventory == null) _lstLocationInventory = new List<LocationInventoryInfo>();
                return _lstLocationInventory;
            }
            set { value = _lstLocationInventory; }
        }
        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
 
                string gbCode = txtGoodsCode.Text.Trim();

                if (string.IsNullOrEmpty(gbCode))
                {
                    MessageBox.Show("请扫描商品条码！");
                    txtGoodsCode.SelectAll();
                    return;
                }

                LocationInventoryInfo _LocationInventory = Duty.ExpiryActivateUIRule.NewInstance.GetInventoryByGoodsCode(gbCode).DATA;

                if (_LocationInventory == null)
                {
                    MessageBox.Show("找不到该商品数据！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                if (_LocationInventory.QUALITYSTATUS == "2")
                {
                    MessageBox.Show("该商品已过保，无法解保！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                if (_LocationInventory.QUALITYSTATUS == "0")
                {
                    MessageBox.Show("该商品为合格商品，无需解保！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                int curIndex = lstLocationInventory.FindIndex(x => x.GOODSBATCHCODE == gbCode);

                if (curIndex >= 0)
                {
                    //this.dtGridGoods.UnSelect(dtGridGoods.CurrentRowIndex);
                    this.dbGridGoods.Select(curIndex);
                 
                    return;
                }
                lstLocationInventory.Add(_LocationInventory);
                this.GoodsSource.DataSource = null;
                this.GoodsSource.DataSource = lstLocationInventory;
                this.dbGridGoods.DataSource = this.GoodsSource;
                this.dbGridGoods.Refresh();
                int addIndex = lstLocationInventory.FindIndex(x => x.GOODSCODE == gbCode);
                if (addIndex >= 0)
                    this.dbGridGoods.Select(addIndex);
 


            }
            catch (Exception ex)
            {
                txtGoodsCode.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            } 
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstLocationInventory != null && lstLocationInventory.Count() > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;

                    Cursor.Current = Cursors.WaitCursor;
                    var re = Duty.ExpiryActivateUIRule.NewInstance.UpdateInventoryQuality(lstLocationInventory);

                    if (re.DATA)
                    {
                        lstLocationInventory.Clear();
                        this.GoodsSource.DataSource = null;
                        this.dbGridGoods.DataSource = this.GoodsSource;
                        txtGoodsCode.Text = string.Empty;
                        txtGoodsCode.Focus();
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交成功！");
                        lstLocationInventory = null;

                    }
                    else
                    {
                        MessageBox.Show("提交失败！");
                        return;
                    }
                }
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void FrmExpiryActivate_Load(object sender, EventArgs e)
        {
            this.txtGoodsCode.Focus();
        }

        private void FrmExpiryActivate_Closed(object sender, EventArgs e)
        {
            _lstLocationInventory = null;
        }
    }
}